<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\store\model;

use app\common\model\Reservation as ReservationModel;
use \think\db;
/**
 * 文章模型
 * Class Article
 * @package app\store\model
 */
class Reservation extends ReservationModel
{
    /**
     * 获取列表
     * @param array $param
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getList(array $param = [])
    {
        // 查询参数
        $params = $this->setQueryDefaultValue($param, [
            'reservation_name' => '',    // 预约人
            'reservation_project' => '',    // 预约项目
            'reservation_status' => -1,    // 预约状态
        ]);
        // 检索查询条件
        $filter = [];
        // 预约人
        !empty($params['reservation_name']) && $filter[] = ['reservation_name', 'like', "%{$params['reservation_name']}%"];
        // 预约项目
        $params['reservation_project']<>"全部" && $filter[] = ['reservation_project', 'like',"%{$params['reservation_project']}%"];
        // 预约状态
        $params['reservation_status'] > -1 && $filter[] = ['reservation_status', '=', $params['reservation_status']];
        // 查询列表数据
        return $this
            ->where($filter)
            ->where('is_deleted', '=', 0)
            ->order(['reservation_create_time' => 'desc'])
            ->paginate(15);
    }

    /**
     * 新增记录
     * @param array $data
     * @return false|int
     */
    public function add(array $data)
    {
        if (empty($data['reservation_name'])) {
            $this->error = '请填写名字';
            return false;
        }
        if (empty($data['reservation_phone'])) {
            $this->error = '请填写手机';
            return false;
        }
        if (empty($data['reservation_time'])) {
            $this->error = '请填写时间';
            return false;
        }
        if (empty($data['reservation_project'])) {
            $this->error = '请填写项目';
            return false;
        }
        if (empty($data['reservation_docter'])) {
            $this->error = '请填写医生';
            return false;
        }

        $data['store_id'] = self::$storeId;
        $data['reservation_status'] = 0;
        $data['reservation_time'] = str2time_datetime($data['reservation_time']);
        $data['reservation_create_time'] =time();
        $data['reservation_update_time'] = time();
        return $this->save($data);
    }

    /**
     * 文章详情
     * @param int $articleId
     * @param array $with
     * @return array|null|static
     */
    public static function detail(int $articleId, array $with = [])
    {
        return self::get($articleId, $with);
    }

    /**
     * 软删除
     * @return false|int
     */
    public function setDelete()
    {
        return $this->save(['is_deleted' => 1]);
    }

    /**
     * 更新预约状态
     * @return false|int
     */
    public function setReservationStatus($reservationStatus)
    {
        return $this->save(['reservation_status' => $reservationStatus]);
    }


    /**
     * 更新记录
     * @param array $data
     * @return bool|int
     */
    public function edit(array $data)
    {
        if (empty($data['reservation_name'])) {
            $this->error = '请填写名字';
            return false;
        }
        if (empty($data['reservation_phone'])) {
            $this->error = '请填写手机';
            return false;
        }
        if (empty($data['reservation_time'])) {
            $this->error = '请填写时间';
            return false;
        }
        if (empty($data['reservation_project'])) {
            $this->error = '请填写项目';
            return false;
        }
        if (empty($data['reservation_docter'])) {
            $this->error = '请填写医生';
            return false;
        }

        $data['store_id'] = self::$storeId;
        $data['reservation_time'] = str2time_datetime($data['reservation_time']);
        $data['reservation_update_time'] = time();
        return $this->save($data) !== false;
    }

    /**
     * 更新状态
     * @param array $data
     * @return bool|int
     */
    public function updateStatus(array $data)
    {
        if (empty($data['reservation_status'])) {
            $this->error = '状态传输异常';
            return false;
        }

        $data['store_id'] = self::$storeId;
        $data['reservation_update_time'] = time();
        return $this->save($data) !== false;
    }
}
